/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.SortedMap;
/*     */ import java.util.TreeMap;
/*     */ import java.util.TreeSet;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ContactRankings
/*     */ {
/*     */   private static final String CONFIG_KEY_CONTACT_RANKINGS = "CONTACT_RANKINGS";
/*     */   private static final String KEY_NAME = "n";
/*     */   private static final String KEY_RANKING = "r";
/*     */   private static final String KEY_LAST_ACCESSED = "t";
/*     */   private int mTableSize;
/*     */   private String mAccountId;
/*     */   private TreeMap<String, TreeSet<ContactAutoComplete.ContactEntry>> mEntryMap;
/*     */   private HashMap<String, ContactAutoComplete.ContactEntry> mEntries;
/*     */   
/*     */   public ContactRankings(String accountId)
/*     */     throws ServiceException
/*     */   {
/*  49 */     this.mAccountId = accountId;
/*  50 */     this.mEntryMap = new TreeMap();
/*  51 */     this.mEntries = new HashMap();
/*  52 */     this.mTableSize = Provisioning.getInstance().get(Key.AccountBy.id, this.mAccountId).getIntAttr("zimbraContactRankingTableSize", 40);
/*  53 */     if (!LC.contact_ranking_enabled.booleanValue())
/*  54 */       return;
/*  55 */     readFromDatabase();
/*     */   }
/*     */   
/*  58 */   public static void reset(String accountId) throws ServiceException { if (!LC.contact_ranking_enabled.booleanValue())
/*  59 */       return;
/*  60 */     ContactRankings rankings = new ContactRankings(accountId);
/*  61 */     rankings.mEntryMap.clear();
/*  62 */     rankings.mEntries.clear();
/*  63 */     rankings.writeToDatabase();
/*     */   }
/*     */   
/*  66 */   public static void remove(String accountId, String email) throws ServiceException { if (!LC.contact_ranking_enabled.booleanValue())
/*  67 */       return;
/*  68 */     ContactRankings rankings = new ContactRankings(accountId);
/*  69 */     ContactAutoComplete.ContactEntry entry = (ContactAutoComplete.ContactEntry)rankings.mEntries.get(email.toLowerCase());
/*  70 */     if (entry != null)
/*  71 */       rankings.remove(entry);
/*  72 */     rankings.writeToDatabase();
/*     */   }
/*     */   
/*     */   public static void increment(String accountId, Collection<? extends Address> addrs) throws ServiceException {
/*  76 */     if (!LC.contact_ranking_enabled.booleanValue())
/*  77 */       return;
/*  78 */     ContactRankings rankings = new ContactRankings(accountId);
/*  79 */     for (Address addr : addrs) {
/*  80 */       if ((addr instanceof InternetAddress)) {
/*  81 */         InternetAddress address = (InternetAddress)addr;
/*  82 */         rankings.increment(address.getAddress(), address.getPersonal());
/*     */       }
/*     */     }
/*  85 */     rankings.writeToDatabase();
/*     */   }
/*     */   
/*     */   public static void increment(String accountId, Address[] addrs) throws ServiceException {
/*  89 */     HashSet<Address> addrSet = new HashSet();
/*  90 */     Collections.addAll(addrSet, addrs);
/*  91 */     increment(accountId, addrSet);
/*     */   }
/*     */   
/*     */   public synchronized void increment(String email, String displayName) {
/*  95 */     long now = System.currentTimeMillis();
/*  96 */     email = email.toLowerCase();
/*  97 */     ContactAutoComplete.ContactEntry entry = (ContactAutoComplete.ContactEntry)this.mEntries.get(email.toLowerCase());
/*  98 */     if (entry == null) {
/*  99 */       entry = new ContactAutoComplete.ContactEntry();
/* 100 */       entry.mEmail = email;
/* 101 */       entry.setName(displayName);
/* 102 */       entry.mRanking = 1;
/* 103 */       entry.mFolderId = -1;
/* 104 */       entry.mLastAccessed = now;
/*     */       
/* 106 */       if (this.mEntries.size() >= this.mTableSize) {
/* 107 */         ContactAutoComplete.ContactEntry lastEntry = (ContactAutoComplete.ContactEntry)getSortedSet().last();
/* 108 */         if (lastEntry.mRanking < 1) {
/* 109 */           remove(lastEntry);
/*     */         }
/*     */       }
/* 112 */       if (this.mEntries.size() < this.mTableSize) {
/* 113 */         add(entry);
/*     */       } else {
/* 115 */         for (ContactAutoComplete.ContactEntry e : this.mEntries.values()) {
/* 116 */           int weeksOld = (int)((now - e.mLastAccessed) / 604800000L) + 1;
/* 117 */           e.mRanking -= weeksOld;
/* 118 */           if (e.mRanking < 0)
/* 119 */             e.mRanking = 0;
/*     */         }
/*     */       }
/*     */     } else {
/* 123 */       entry.mRanking += 1;
/* 124 */       if (entry.mRanking <= 0)
/* 125 */         entry.mRanking = 1;
/* 126 */       if ((displayName != null) && (displayName.length() > 0))
/* 127 */         entry.setName(displayName);
/* 128 */       entry.mLastAccessed = now;
/*     */     }
/*     */   }
/*     */   
/* 132 */   public int query(String email) { ContactAutoComplete.ContactEntry entry = (ContactAutoComplete.ContactEntry)this.mEntries.get(email.toLowerCase());
/* 133 */     if (entry != null)
/* 134 */       return entry.mRanking;
/* 135 */     return 0;
/*     */   }
/*     */   
/* 138 */   public synchronized Collection<ContactAutoComplete.ContactEntry> search(String str) { TreeSet<ContactAutoComplete.ContactEntry> entries = new TreeSet();
/* 139 */     int len = str.length();
/* 140 */     for (String k : this.mEntryMap.tailMap(str).keySet()) {
/* 141 */       if ((k.length() < len) || (!k.substring(0, len).equalsIgnoreCase(str)))
/*     */         break;
/* 143 */       entries.addAll((Collection)this.mEntryMap.get(k));
/*     */     }
/*     */     
/*     */ 
/* 147 */     return entries;
/*     */   }
/*     */   
/* 150 */   private synchronized TreeSet<ContactAutoComplete.ContactEntry> getSortedSet() { return new TreeSet(this.mEntries.values()); }
/*     */   
/*     */   private synchronized void readFromDatabase() throws ServiceException {
/* 153 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(this.mAccountId);
/* 154 */     Metadata config = mbox.getConfig(null, "CONTACT_RANKINGS");
/* 155 */     if (config == null) {
/* 156 */       config = new Metadata();
/* 157 */       mbox.setConfig(null, "CONTACT_RANKINGS", config);
/*     */     }
/* 159 */     for (Map.Entry<Object, Object> entry : config.map.entrySet()) {
/* 160 */       if ((entry.getValue() instanceof Map)) {
/* 161 */         Map<?, ?> m = (Map)entry.getValue();
/* 162 */         ContactAutoComplete.ContactEntry contact = new ContactAutoComplete.ContactEntry();
/* 163 */         contact.mEmail = ((String)entry.getKey()).toLowerCase();
/* 164 */         Long num = (Long)m.get("r");
/* 165 */         contact.mRanking = num.intValue();
/* 166 */         num = (Long)m.get("t");
/* 167 */         contact.mLastAccessed = num.longValue();
/* 168 */         contact.setName((String)m.get("n"));
/* 169 */         contact.mFolderId = -1;
/* 170 */         add(contact);
/*     */       }
/*     */     }
/* 173 */     dump("reading");
/*     */   }
/*     */   
/* 176 */   private synchronized void writeToDatabase() throws ServiceException { Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(this.mAccountId);
/* 177 */     Metadata config = new Metadata();
/* 178 */     for (ContactAutoComplete.ContactEntry entry : getSortedSet()) {
/* 179 */       Metadata m = new Metadata();
/* 180 */       m.put("r", entry.mRanking);
/* 181 */       if (entry.mDisplayName != null)
/* 182 */         m.put("n", entry.mDisplayName);
/* 183 */       m.put("t", entry.mLastAccessed);
/* 184 */       config.put(entry.mEmail, m);
/*     */     }
/* 186 */     mbox.setConfig(null, "CONTACT_RANKINGS", config);
/* 187 */     dump("writing");
/*     */   }
/*     */   
/* 190 */   private synchronized TreeSet<ContactAutoComplete.ContactEntry> get(String str) { TreeSet<ContactAutoComplete.ContactEntry> val = (TreeSet)this.mEntryMap.get(str.toLowerCase());
/* 191 */     if (val == null) {
/* 192 */       val = new TreeSet();
/* 193 */       this.mEntryMap.put(str.toLowerCase(), val);
/*     */     }
/* 195 */     return val;
/*     */   }
/*     */   
/* 198 */   private synchronized void add(ContactAutoComplete.ContactEntry entry) { get(entry.mEmail).add(entry);
/* 199 */     if (entry.mDisplayName.length() > 0)
/* 200 */       get(entry.mDisplayName).add(entry);
/* 201 */     if (entry.mLastName.length() > 0)
/* 202 */       get(entry.mLastName).add(entry);
/* 203 */     this.mEntries.put(entry.mEmail.toLowerCase(), entry);
/*     */   }
/*     */   
/* 206 */   private synchronized void remove(ContactAutoComplete.ContactEntry entry) { get(entry.mEmail).remove(entry);
/* 207 */     if (entry.mDisplayName.length() > 0)
/* 208 */       get(entry.mDisplayName).remove(entry);
/* 209 */     if (entry.mLastName.length() > 0)
/* 210 */       get(entry.mLastName).remove(entry);
/* 211 */     this.mEntries.remove(entry.mEmail.toLowerCase());
/*     */   }
/*     */   
/* 214 */   private void dump(String action) { if (ZimbraLog.gal.isDebugEnabled()) {
/* 215 */       StringBuilder buf = new StringBuilder(action + " contact rankings");
/* 216 */       buf.append("\n");
/* 217 */       for (ContactAutoComplete.ContactEntry entry : getSortedSet()) {
/* 218 */         entry.toString(buf);
/* 219 */         buf.append("\n");
/*     */       }
/* 221 */       ZimbraLog.gal.debug(buf.toString());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/ContactRankings.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */